var $ = jQuery;
jQuery(function ($) {
	// Deactive
	$(".deactive").each(function () {
		$(this).parents("tr").addClass("deactive");
	});

	var loading = jQuery("<img class='loading' src='images/wpspin_light.gif' />");
	// Chosen selects
	$("select.chosen_select").chosen();

	// Tooltips
	jQuery(".tips, .help_tip").tipTip({
		'attribute': 'data-tip',
		'fadeIn': 50,
		'fadeOut': 50,
		'delay': 200
	});

	jQuery('a[data-toggle="tab"]').on('shown', function (e) {
		e.target
		e.relatedTarget
	});


	// Media upload handleEvent
	// Uploading files
	var file_frame;
	$(".forminp-upload .btn-media-upload").on("click", function (event) {
		event.preventDefault();
		var btnClick = $(this);
		var parent = btnClick.parent();
		if (file_frame) {
			file_frame.open();
			return false;
		}
		file_frame = wp.media.frames.file_frame = wp.media({
			title: jQuery(this).data('uploader_title'),
			button: {
				text: jQuery(this).data('uploader_button_text'),
			},
			multiple: false
		});
		file_frame.on("select", function () {
			attachment = file_frame.state().get('selection').first().toJSON();
			parent.find("input").val(attachment.id);
			var img = parent.find(".img-preview").children("img");
			if (img.length <= 0) {
				img = $("<img class='thumbnail' />");
			}
			img.attr("src", attachment.url);
			parent.find(".img-preview").html("").append(img);

			// btn remove
			$("<a href='#' class='remove-upload-image' />").html("<i class='icon-minus-sign icon-large color-red'></i>")
				.appendTo(parent.find(".img-preview")).css({
					'position': 'absolute',
					'top': "-10px",
					'right': "-8px"
				});
		});
		// Finally, open the modal
		file_frame.open();
		return false;
	});

	// Handle click
	$(".remove-upload-image").live("click", function () {
		$(this).parent().find("img").animate({
			opacity: 0
		}, 500, function () {
			$(this).remove()
		});
		$(this).parent(".img-preview").parent(".forminp-upload").children("input").val("");
		$(this).remove();
		return false;
	});
	
	// Drag and Drop Group/field
	// -- Groups
	$(".draggable[data_type='group']").draggable({
		connectToSortable: "#container-groups",
		helper: "clone",
		revert: false,
		appendTo: "body",
		drag: function(event, ui ){
			ui.helper.css({
				"list-style":"none",
				"width": $("#container-groups").width(),
				"z-index": "99"
			});
			ui.helper.find(".widget").removeClass("hide");
			ui.helper.find(".open").hide();
		},
	});

	$("#container-groups").droppable({
		activeClass: "ui-state-default",
		hoverClass: "ui-state-hover",
		accept: ".draggable[data_type='group']",
		drop: function (event, ui) {
			ui.draggable.addClass("sortable");
			ui.draggable.find(".open").remove();
			ui.draggable.find(".widget").removeClass("hide");
			ui.draggable.find(".widget-content").show();
			ui.draggable.find(".container").droppable({
				activeClass: "ui-state-default",
				hoverClass: "ui-state-hover",
				accept: ".draggable[data_type='field']",
				drop: function (event, ui) {
					ui.draggable.find(".link-field").remove();
					ui.draggable.find(".html-field").show();
				},
			}).sortable({
				items: ".draggable",
				drag: ".drag",
				connectWith: ".container",
			});
		},
	}).sortable({
		items: "div.widget",
		sort: function (event, ui) {
			$(this).removeClass("ui-state-default");
		}
	});
	
	// -- Fields
	$(".draggable[data_type='field']").draggable({
		connectToSortable: "ul.container",
		helper: "clone",
		revert: false,
		appendTo: "body",
		drag: function(event, ui ){
			ui.helper.find(".link-field").remove();
			ui.helper.find(".html-field").removeClass("hide");
			ui.helper.css({
				"list-style":"none",
				"width": $("#container-groups").width()-50,
				"z-index": "99"
			});
		},
	});

	// Change name group
	$(".group-name.input-small").keyup(function(e){
		$(this).parents(".groups").find(".change-name").val(this.value);
	});
	
	// remove group
	$(".widget .gclose").live("click", function () {
		$(this).parents(".widget").fadeOut("fast", function () {
			$(this).remove();
		});
		return false;
	});

	// toggle widget group
	$(".widget .gminimize").live("click", function () {
		$(this).parents(".widget").children(".widget-content").slideToggle();
		$(this).children(".icon-chevron-up").toggleClass("icon-chevron-down");
	});
	
	// Submit form and create array form data group/field
	$(".updateform").submit(function(){
		return false;
	});
    
    
});

// Remove field
function remove_field(obj){
    $(obj).parents(".fields[data_type='field']").fadeOut("slow",function(){
        $(this).remove();
    });
    return false;
}

// Toogle advanced setting field
function toggle_advanced_field(obj){
    $(obj).parents(".fields").children(".field-advanced-setting").slideToggle("slow");
    $(obj).children("i").toggleClass("icon-chevron-up").toggleClass("icon-chevron-down");
    return false;
}